package com.bw.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.bw.entity.Dict;
import com.bw.entity.JobAddParam;
import com.bw.entity.JobInfo;
import com.bw.entity.JobQuery;
import com.bw.mapper.JobMapper;
import com.bw.service.JobService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.RegEx;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class ListController {
    @Autowired
    JobMapper jobMapper;

    @Autowired
    JobService jobService;

    @RequestMapping("/list.do")
    public String list(JobQuery query, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        // 1. 给列表页的下拉框赋值
        selectData(model);
        //2. 分页查询

        PageHelper.startPage(pageNum, 10);
        List<JobInfo> list = jobMapper.listPage(query);
        PageInfo<JobInfo> pageInfo = new PageInfo<>(list);

        model.addAttribute("pageInfo", pageInfo);


        //3. 条件回显

        model.addAttribute("query", query);

        return "list";
    }


    @RequestMapping("/del.do")
    public String del(String id) {

        jobMapper.del(id);
        return "redirect:list.do";
    }

    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        selectData(model);
        return "add";
    }

    @RequestMapping("/add.do")
    public String add(@Valid JobAddParam addParam, BindingResult result,Model model) {
        // 1.非空验证
        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            // 给下拉框赋值
            selectData(model);
            return "add";

        }
        //员工编号以“GH”开始
        String num = addParam.getNum();
        if (!num.startsWith("GH")){
            model.addAttribute("error","员工编号以“GH”开始");
            selectData(model);
            return "add";
        }
        // 开始时间 小于 结束时间
        String startTime = addParam.getStartTime();
        DateTime start = DateUtil.parse(startTime, "yyyy-MM-dd");

        String endTime = addParam.getEndTime();
        DateTime end = DateUtil.parse(endTime, "yyyy-MM-dd");

        if (start.isAfter(end)){
            model.addAttribute("error","开始时间必须小于结束时间");
            selectData(model);
            return "add";

        }

        //保存方法事务
        jobService.add(addParam);

        return "redirect:list.do";
    }


    // 下拉框方法封装起来了
    private void selectData(Model model) {

        List<Dict> postList = jobMapper.listPost();
        model.addAttribute("postList", postList);
        List<Dict> schoolList = jobMapper.listschool();
        model.addAttribute("schoolList", schoolList);
        List<Dict> deptList = jobMapper.listDept();
        model.addAttribute("deptList", deptList);

    }
}
